# producer.py
import pika
import json

def publish_order(order_id):
    connection_params = pika.ConnectionParameters(
        host='localhost',
        virtual_host='order_vhost',
        credentials=pika.PlainCredentials('admin', '123')
    )
    connection = pika.BlockingConnection(connection_params)
    channel = connection.channel()

    # 定义死信队列和交换机
    channel.exchange_declare(exchange='dead_letter_exchange', exchange_type='fanout')
    channel.queue_declare(queue='dead_letter_queue', durable=True)
    channel.queue_bind(exchange='dead_letter_exchange', queue='dead_letter_queue')

    # 定义普通队列并设置30秒消息超时时间和死信交换机
    channel.queue_declare(queue='normal_queue', arguments={'x-message-ttl': 30000,  # 设置30秒消息超时时间
                                                           'x-dead-letter-exchange': 'dead_letter_exchange'})

    # 发送消息到普通队列
    message = {'order_id': order_id}
    channel.basic_publish(exchange='', routing_key='normal_queue', body=json.dumps(message))

    print(f"Order {order_id} published to normal_queue")

    connection.close()

if __name__ == '__main__':
    # 生产者发布订单
    order_id = 123
    publish_order(order_id)
